<?php
class Language {
  	var $code;
  	var $languages = array();
  	var $data      = array();

  	function __construct(&$locator) {
		$this->config   =& $locator->get('config');
		$this->database =& $locator->get('database');
		$this->request  =& $locator->get('request');
		$this->session  =& $locator->get('session');
								
    	$results = $this->database->cache('language', "select * from language");

    	foreach ($results as $result) {
      		$this->languages[$result['code']] = array(
        		'language_id' => $result['language_id'],
        		'name'        => $result['name'],
        		'code'        => $result['code'],
				'directory'   => $result['directory'],
				'filename'    => $result['filename'],
      		);
    	}
 		
    	if (array_key_exists($this->session->get('language'), $this->languages)) {
      		$this->set($this->session->get('language'));
    	} elseif (array_key_exists($this->request->get('language', 'cookie'), $this->languages)) {
      		$this->set($this->request->get('language', 'cookie'));
    	} elseif ($browser = $this->detect()) {
	    	$this->set($browser);
	  	} else {
        	$this->set($this->config->get('config_language'));
		}
	
		$this->load($this->languages[$this->code]['filename']);
  	}
	
	function set($language) {
    	$this->code = $language;
		
    	if ((!$this->session->has('language')) || ($this->session->get('language') != $language)) {
      		$this->session->set('language', $language);
    	}

    	if ((!$this->request->get('language', 'cookie')) || ($this->request->get('language', 'cookie') != $language)) {	  
	  		setcookie('language', $language, time() + 60 * 60 * 24 * 30, '/', $_SERVER['HTTP_HOST']);
    	}
  	}
    
	function load($filename, $directory = DIR_LANGUAGE) {
    	$file = $directory . $this->languages[$this->code]['directory'] . '/' . $filename;
    
    	if (file_exists($file)) {
	  		$_ = array();
	  
	  		include($file);
	  
      		$this->data = array_merge($this->data, $_);
    	} else {
      		exit('Error: Language file ' . $file . ' not found!');
    	}
  	}
  
  	function get($key) {
    	$args = func_get_args();
 
    	if (count($args) > 1) {
      		return vsprintf($this->get(array_shift($args)), $args);
    	} else {
      		return (isset($this->data[$key]) ? $this->data[$key] : $key);
    	}
  	}

  	function detect() {
    	if (@$_SERVER['HTTP_ACCEPT_LANGUAGE']) { 
      		$browser_languages = explode(',', @$_SERVER['HTTP_ACCEPT_LANGUAGE']);
	  
      		$languages = array(
        		'ar' => 'ar([-_][[:alpha:]]{2})?|arabic',
        		'bg' => 'bg|bulgarian',
        		'br' => 'pt[-_]br|brazilian portuguese',
        		'ca' => 'ca|catalan',
        		'cs' => 'cs|czech',
        		'da' => 'da|danish',
        		'de' => 'de([-_][[:alpha:]]{2})?|german',
        		'el' => 'el|greek',
       		 	'en' => 'en([-_][[:alpha:]]{2})?|english',
        		'es' => 'es([-_][[:alpha:]]{2})?|spanish',
        		'et' => 'et|estonian',
        		'fi' => 'fi|finnish',
        		'fr' => 'fr([-_][[:alpha:]]{2})?|french',
        		'gl' => 'gl|galician',
        		'he' => 'he|hebrew',
        		'hu' => 'hu|hungarian',
        		'id' => 'id|indonesian',
        		'it' => 'it|italian',
        		'ja' => 'ja|japanese',
        		'ko' => 'ko|korean',
       		 	'ka' => 'ka|georgian',
        		'lt' => 'lt|lithuanian',
        		'lv' => 'lv|latvian',
        		'nl' => 'nl([-_][[:alpha:]]{2})?|dutch',
        		'no' => 'no|norwegian',
        		'pl' => 'pl|polish',
        		'pt' => 'pt([-_][[:alpha:]]{2})?|portuguese',
        		'ro' => 'ro|romanian',
        		'ru' => 'ru|russian',
        		'sk' => 'sk|slovak',
        		'sr' => 'sr|serbian',
        		'sv' => 'sv|swedish',
        		'th' => 'th|thai',
        		'tr' => 'tr|turkish',
        		'uk' => 'uk|ukrainian',
        		'tw' => 'zh[-_]tw|chinese traditional',
        		'zh' => 'zh|chinese simplified'
      		);
 
      		foreach ($browser_languages as $browser_language) {
        		foreach ($languages as $key => $value) {
          			if (eregi('^(' . $value . ')(;q=[0-9]\\.[0-9])?$', $browser_language)) {
					    if (isset($this->languages[$key])) {
            			  return $key;
						}
          			}
        		}
      		}
    	}

    	return FALSE;
	}
    
  	function getId() {
    	return $this->languages[$this->code]['language_id'];
  	}
  
  	function getCode() {
    	return $this->code;
  	}  
}
?>
